package com.demomaster.quickcompat.view;


import android.app.Activity;
import android.content.res.Resources;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import java.util.HashMap;
import java.util.Map;

public class QuickView {
    private int id = View.NO_ID;
    private String tag;
    private int resId= Resources.ID_NULL;
    private float x;
    private float y;
    private int top;
    private int left;
    private int topMargin;
    private int leftMargin;
    private int zIndex;
    private Map<String,String> activityMap;
    private FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    public int getTop() {
        return top;
    }

    public void setTop(int top) {
        this.top = top;
    }

    public int getLeft() {
        return left;
    }

    public void setLeft(int left) {
        this.left = left;
    }

    public QuickView() {
        activityMap = new HashMap<>();
    }

    public FrameLayout.LayoutParams getLayoutParams() {
        return layoutParams;
    }

    public void setLayoutParams(FrameLayout.LayoutParams layoutParams) {
        this.layoutParams = layoutParams;
    }

    public int getResId() {
        return resId;
    }

    public void setResId(int resId) {
        this.resId = resId;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public int getzIndex() {
        return zIndex;
    }

    public void setzIndex(int zIndex) {
        this.zIndex = zIndex;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getTopMargin() {
        return topMargin;
    }

    public void setTopMargin(int topMargin) {
        this.topMargin = topMargin;
    }

    public int getLeftMargin() {
        return leftMargin;
    }

    public void setLeftMargin(int leftMargin) {
        this.leftMargin = leftMargin;
    }

    public View getView(Activity activity) {
        View view = null;
        if(resId!= Resources.ID_NULL){
            view = activity.getLayoutInflater().inflate(resId,null);
            if(view.getId()==View.NO_ID){
                if(id==View.NO_ID) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                        id = View.generateViewId();
                    }
                }
                view.setId(id);
            }else {
                id=view.getId();
            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            view.setZ(zIndex);
        }
        view.setTag(tag);
        String activityId = activity.getClass().getName()+"-"+activity.hashCode();
        activityMap.put(activityId,"");
        return view;
    }
    public void unBindActivity(Activity activity){
        String activityId = activity.getClass().getName()+"-"+activity.hashCode();
        activityMap.remove(activityId);
    }
    OnViewLoad onViewLoad;
    public void setOnViewLoad(OnViewLoad onViewLoad){
        this.onViewLoad = onViewLoad;
    }
    public interface OnViewLoad{
        //void generateView();
        void onInflatView(ViewGroup viewGroup, View view);
    }
}
